<!--
 @description 零售商客户
 @fileName RetailCustomer.vue
 @author zengqiongying
 @created 2023/04/15 13:34:39
-->
<template>
  <List ref="qmpList" v-model:finished="finished" :reset-fun="resetData" :load="load" :refresh="load">
    <Item v-for="(item, i) in list" :key="i" :item-data="item" @detail="toDetail(item)" />
  </List>
</template>

<script lang="ts">
  import { ref, reactive, toRefs, defineComponent, watch } from 'vue'
  import { useRouter } from 'vue-router'
  import List from '@/components/List/index.vue'
  import Item from './Item.vue'
  import { getCtmMdmCustomerList } from '@/apis/dealerMarketing/Customer'

  export default defineComponent({
    name: 'RetailCustomer',
    components: { List, Item },
    props: {
      searchValue: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        default: ''
      }
    },
    setup(props) {
      const qmpList = ref()
      const $router = useRouter()
      const datas = reactive({
        list: [],
        finished: false
      })
      const load = async (page: number, rows: number) => {
        let paramsRaw = {}
        if (props.searchValue) {
          paramsRaw = {
            paramKeyword: props.searchValue
          }
        }
        const filtersRaw = []
        if (props.type === 'lightCustomer') {
          // 轻居产品查询
          filtersRaw.push({ id: 'oneCategoryCode0', property: 'oneCategoryCode', value: 'Z230', operator: '=' })
        } else {
          filtersRaw.push({ id: 'oneCategoryCode0', property: 'oneCategoryCode', value: '[Z230]', operator: 'not in' })
        }
        const paramsData = {
          page,
          rows,
          sort: 'creationTime',
          filtersRaw,
          paramsRaw,
          sortersRaw: []
        }
        return getCtmMdmCustomerList(paramsData).then((res) => {
          const resData = res.data
          const list = resData.records
          if (page === 1) {
            datas.list = list
          } else {
            datas.list = datas.list.concat(list)
          }
          if (resData.total <= datas.list.length) {
            datas.finished = true
          }
        })
      }
      const resetData = () => {
        datas.list = []
      }
      const resetPageData = () => {
        qmpList.value.resetPageData()
      }
      const toDetail = (row: any) => {
        $router.push({
          path: `/dealerCustomer/${props.type === 'lightCustomer' ? 'lightCustomerDetail' : 'retailCustomerDetail'}`,
          query: {
            code: row.code
          }
        })
      }

      watch(
        () => props.searchValue,
        () => {
          resetPageData()
        }
      )
      return {
        ...toRefs(datas),
        load,
        resetData,
        qmpList,
        resetPageData,
        toDetail
      }
    }
  })
</script>
<style lang="less" scoped></style>
